import db from '@dfeidao/atom-nodejs/db';
import log from '@dfeidao/atom-nodejs/logger/log';
import msg from '@dfeidao/atom-nodejs/msg';
import { IncomingHttpHeaders } from 'http';
import { systemid } from '../atom/config';

interface Message {
	quote_job_name: string;
	contents_homework: string;
	student_name: string;
	student_no: string;
	productid: string;
	teacher_no: string;
	teacher_name: string;
	class_id: string;
	class_name: string;
	// cookie: {
	// 	uk: string;
	// 	[key: string]: string
	// };
	// urls: {
	// 	base: string;
	// 	origin: string;
	// 	url: string;
	// };
	// query: {};
	// params: {};
	// headers: {};
	// captcha: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string;
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(message: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {
	log('Service begin path:app-personal-work/s002,action_id:' + action_id);

	const { exec, prepare } = db.query(action_id, session_id, systemid);

	prepare('teacher', ['subject_name', 'subject_num'], { productid: message.productid, teacher_no: message.teacher_no }, 1, 1, [], []);// 科目编号 科目名称

	prepare('class', ['grade_no', 'grade'], { productid: message.productid }, 199, 1, [], []);// 年级编号 年级名称

	const [dt, data] = await exec<{ subject_num: string, subject_name: string }, { grade_no: string, grade: string }>();

	const { exec: ec, insert } = db.operate(action_id, session_id, systemid);
	const code = await msg.doc_code(action_id, session_id, systemid, 'BM0016062', '', 1);
	const code1 = await msg.doc_code(action_id, session_id, systemid, 'BM0016083', '', 1);
	const job_no = code.result;// 作业编号
	const stu_task_detail_num = code1.result;// 作业明细编号
	const date = await msg.sys_date(action_id, session_id);

	insert('task', {
		job_no,
		subject_name: dt[0].subject_name,
		subject_num: dt[0].subject_num,
		contents_homework: message.contents_homework,
		quote_job_name: message.quote_job_name,
		teacher_no: message.teacher_no,
		teacher_name: message.teacher_name,
		class_id: message.class_id,
		class_name: message.class_name,
		personalized_setting: '1',
		task_type: '0',
		grade: data[0].grade,
		grade_no: data[0].grade_no,
		create_time: date,
		productid: message.productid
	});

	insert('stu_task_detail', {
		stu_task_detail_num,
		job_no,
		quote_job_name: message.quote_job_name,
		contents_homework: message.contents_homework,
		student_name: message.student_name,
		student_no: message.student_no,
		job_status: '0',
		submit_status: '0',
		submission_time: date,
		subject_name: dt[0].subject_name,
		subject_num: dt[0].subject_num,
		productid: message.productid,
		submit_contents: '',
		read_concent: '',
		reader: '',
		read_time: 0
	});
	const ret = await ec();
	log('Service end path:app-personal-work/s002,action_id:' + action_id);
	return {
		data: ret
	};
}
